<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="因为群组可能是用户群组或者答者群组，因此这里设置了personnel_id这个键和用户或者答者进行关联" prop="personnelId">
      <el-input v-model="dataForm.personnelId" placeholder="因为群组可能是用户群组或者答者群组，因此这里设置了personnel_id这个键和用户或者答者进行关联"></el-input>
    </el-form-item>
    <el-form-item label="群组对应的类型，相当于标签，比如教师群组，健身爱好者群组" prop="kind">
      <el-input v-model="dataForm.kind" placeholder="群组对应的类型，相当于标签，比如教师群组，健身爱好者群组"></el-input>
    </el-form-item>
    <el-form-item label="群组标识符(用户群组或者答者群组)，用户群组标识符为0，答者群组标识符为1" prop="sign">
      <el-input v-model="dataForm.sign" placeholder="群组标识符(用户群组或者答者群组)，用户群组标识符为0，答者群组标识符为1"></el-input>
    </el-form-item>
    <el-form-item label="0代表未删除，1代表已经删除" prop="isDeleted">
      <el-input v-model="dataForm.isDeleted" placeholder="0代表未删除，1代表已经删除"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          personnelId: '',
          kind: '',
          sign: '',
          isDeleted: ''
        },
        dataRule: {
          personnelId: [
            { required: true, message: '因为群组可能是用户群组或者答者群组，因此这里设置了personnel_id这个键和用户或者答者进行关联不能为空', trigger: 'blur' }
          ],
          kind: [
            { required: true, message: '群组对应的类型，相当于标签，比如教师群组，健身爱好者群组不能为空', trigger: 'blur' }
          ],
          sign: [
            { required: true, message: '群组标识符(用户群组或者答者群组)，用户群组标识符为0，答者群组标识符为1不能为空', trigger: 'blur' }
          ],
          isDeleted: [
            { required: true, message: '0代表未删除，1代表已经删除不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/questionnaire/group/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.personnelId = data.group.personnelId
                this.dataForm.kind = data.group.kind
                this.dataForm.sign = data.group.sign
                this.dataForm.isDeleted = data.group.isDeleted
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/questionnaire/group/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'personnelId': this.dataForm.personnelId,
                'kind': this.dataForm.kind,
                'sign': this.dataForm.sign,
                'isDeleted': this.dataForm.isDeleted
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
